package com.qingzhuge.pay.utils;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.qingzhuge.pay.entity.SysPayProperty;
import org.apache.commons.lang3.StringUtils;

/**
 * @author : zero.xiao
 * @description :
 * @date : 2018/11/15 0015 下午 17:47
 * @modified :
 * @since :
 */
public class PayWxServiceFactory {
    public static WxPayService build(SysPayProperty properties) {
        if (properties == null) {
            return null;
        }
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(properties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(properties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(properties.getMchKey()));
        payConfig.setSubAppId(StringUtils.trimToNull(properties.getSubAppId()));
        payConfig.setSubMchId(StringUtils.trimToNull(properties.getSubMchId()));
        payConfig.setKeyPath(StringUtils.trimToNull(properties.getKeyPath()));
        payConfig.setKeyContent(properties.getKeyContent());
        payConfig.setNotifyUrl(StringUtils.trimToNull(properties.getNotifyUrl()));
        payConfig.setTradeType(StringUtils.trimToNull(properties.getTradeType()));
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}
